import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import ContentView from '../views/ContentView'
import Login from '../views/login/index'
import Reg from '../views/login/reg'
import Forget from '../views/login/forget'
import ManageIndex from "@/views/manage/index";
import TableOnly from "@/components/TableOnlyRead";
import Question from "@/views/Question";

const routes = [
  {
    path: '/',
    component: HomeView,
    redirect: '/main',
    meta: { title: '个人中心' },
    children: [
      {
        path: '/main',
        component: ContentView
      },
      {
        path: '/reg',
        component: Reg,
      },
      {
        path: '/forgot',
        component: Forget,
      },
      {
        path: '/video',
        component: () => import('@/components/UnitVideo'),
      },
      {
        path: '/table',
        component: TableOnly
      },
      {
        path: '/login',
        component: Login,
      },
      {
        path: '/question',
        component: Question,
      },
      {
        path: '/calendar',
        component: () => import('@/components/BigCalendar'),
        meta: { title: '修改密码', requireAuth: true },
      },
      {
        path: '/form_show',
        component: () => import('@/components/FormShow'),
        meta: { title: '修改密码', requireAuth: true },
      },
      {
        path: '/form',
        component: () => import('@/components/FormExam'),
        meta: { title: '修改密码', requireAuth: true },
      },
      {
        path: '/content/:pagename',
        component: () => import('@/components/Content'),
        meta: { title: '个人账户', requireAuth: true },
      },
      {
        path: '/unitcontent/:pagename',
        component: () => import('@/components/UnitContent'),
        meta: { title: '个人账户', requireAuth: true },
      },
    ]
  },

  {
    path: '/main',
    component: ContentView
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/reg',
    component: Reg
  },
  {
    path: '/forgot',
    component: Forget
  },
  {
    path: '/manage_index',
    name: 'manage',
    component: ManageIndex
  },
  {
    path: '/manage',
    redirect: '/manage/content/aa',
    component: ManageIndex,
    meta: { title: '个人中心' },
    children: [
      {
        path: '/manage/content/:pagename',
        component: () => import('@/components/Content'),
        meta: { title: '个人账户', requireAuth: true },
      },
      {
        path: '/manage/form',
        component: () => import('@/components/FormExam'),
        meta: { title: '修改密码', requireAuth: true },
      },
      {
        path: '/manage/form_read',
        component: () => import('@/components/FormRead'),
        meta: { title: '修改密码', requireAuth: true },
      },
      {
        path: '/manage/page_form_read/:pagename',
        component: () => import('@/components/PageFormRead'),
        meta: { title: '个人信息', requireAuth: true },
      },
      {
        path: '/manage/pagetable/:pagename',
        component: () => import('@/components/PageTable'),
        meta: { title: '修改密码', requireAuth: true },
      },
      {
        path: '/manage/passedit',
        component: () => import('@/components/PassEdit'),
        meta: { title: '修改密码', requireAuth: true },
      },
      {
        path: '/manage/pageform',
        component: () => import('@/components/PageForm'),
        meta: { title: '修改密码', requireAuth: true },
      },
      {
        path: '/manage/form_show',
        component: () => import('@/components/FormShow'),
        meta: { title: '修改密码', requireAuth: true },
      },
      {
        path: '/manage/table',
        component: () => import('@/components/Table'),
        meta: { title: '修改密码', requireAuth: true },
      },
      {
        path: '/manage/table_only_modify',
        component: () => import('@/components/TableOnlyModify'),
        meta: { title: '修改密码', requireAuth: true },
      },
      {
        path: '/manage/table_only_read',
        component: () => import('@/components/TableOnlyRead'),
        meta: { title: '修改密码', requireAuth: true },
      },
      {
        path: '/manage/form_edit',
        component: () => import('@/components/FormEdit'),
        meta: { title: '修改密码', requireAuth: true },
      },
    ]
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

// 导航守卫
// 使用 router.beforeEach 注册一个全局前置守卫，判断用户是否登陆
router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next();
  } else {
    if (to.path.indexOf("manage") > 0){
      let token = localStorage.getItem('Authorization');
      if (token === null || token === '') {
        next('/login');
        return;
      } else {
        next();
      }
    }else{
      next();
    }
  }
});
export default router
